﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace tiDE
{
    public partial class FindAndReplace : DockContent
    {
        public FindAndReplace()
        {
            InitializeComponent();
            this.Focus();
            textBox1.Focus();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }

        public event EventHandler<FindAndReplaceEventArgs> ActionRequested;

        private void button1_Click(object sender, EventArgs e)
        {
            // Find Next
            if (ActionRequested != null)
                ActionRequested(this, new FindAndReplaceEventArgs()
                {
                    Action = FindAndReplaceAction.FindNext,
                    Scope = FindAndReplaceScope.CurrentDocument,
                    MatchCase = checkBox1.Checked,
                    UseRegex = checkBox2.Checked,
                    FindText = textBox1.Text,
                });
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // Replace Next
            if (ActionRequested != null)
                ActionRequested(this, new FindAndReplaceEventArgs()
                {
                    Action = FindAndReplaceAction.Replace,
                    Scope = FindAndReplaceScope.CurrentDocument,
                    MatchCase = checkBox4.Checked,
                    UseRegex = checkBox3.Checked,
                    FindText = textBox2.Text,
                    ReplaceText = textBox3.Text,
                });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Replace All
            if (ActionRequested != null)
                ActionRequested(this, new FindAndReplaceEventArgs()
                {
                    Action = FindAndReplaceAction.ReplaceAll,
                    Scope = FindAndReplaceScope.CurrentDocument,
                    MatchCase = checkBox4.Checked,
                    UseRegex = checkBox3.Checked,
                    FindText = textBox2.Text,
                    ReplaceText = textBox3.Text,
                });
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                this.AcceptButton = button1;
            }
            else
            {
                this.AcceptButton = button4;
            }
        }
    }

    public enum FindAndReplaceScope
    {
        CurrentDocument,
        CurrentProject,
        WholeSolution,
    }

    public enum FindAndReplaceAction
    {
        FindNext,
        FindAll,
        Replace,
        ReplaceAll,
    }

    public class FindAndReplaceEventArgs : EventArgs
    {
        public FindAndReplaceScope Scope;
        public FindAndReplaceAction Action;
        public string FindText;
        public string ReplaceText;
        public bool MatchCase, UseRegex;
    }
}
